﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace AutoParkingManagement
{
    public abstract class DLL1356
    {
        [DllImport(@"1356.Dll")]
        public static extern int init_rfid(int iID_RFID);

        [DllImport(@"1356.Dll")]
        public static extern int getUID(int iID_RFID, byte[] csUID);  //khai bao chuoi ky tu 128 char???

        [DllImport(@"1356.Dll")]
        public static extern int get_plate(int iID_RFID, byte[] csPlate_Number,
                                            byte[] csTime_Info, byte[] csDriver, byte[] csVehicle);
        [DllImport(@"1356.Dll")]
        public static extern int write_plate(int iID_RFID, byte[] csPlate_Number,
                                            byte[] csTime_Info, byte[] csDriver, byte[] csVehicle);

        [DllImport(@"1356.Dll")]
        public static extern int init_Cam(int iID_Cam);

        [DllImport(@"1356.Dll")]
        public static extern int get_image(int iID_Cam, ref bool Plate_Flag, ref String Image,
                                            ref int iCorrect, ref int iTime, ref String Time, ref String Plate_number, ref String Vehicle);

        public static string TrimStringByte(string input)
        {
            input = input.Replace(Convert.ToChar(0x0000).ToString(), String.Empty);
            input = input.Replace(Convert.ToChar(0x000A).ToString(), String.Empty);
            return input.Replace(Convert.ToChar(0x000D).ToString(), String.Empty).Trim();
        }

        public static byte[] StringToBytes(string s)
        {
            s = s.Trim();
            byte[] tmp = Encoding.UTF8.GetBytes(s);
            int len = tmp.Length;
            byte[] result = new byte[64];
            for (int i = 0; i < len; i++)
            {
                result[i] = tmp[i];
            }
            result[len] = 10;
            len++;
            result[len] = 13;
            len++;
            for (int i = len; i < 60; i++)
            {
                result[i] = 10;
            }

            for (int i = 60; i < result.Length; i++)
            {
                result[i] = 0;
            }
            return result;
        }

        public static bool GetCardIDByCardReader(int RFID, ref string cardID, ref string message)
        {
            try
            {
                int status;
                int iID_RFID = RFID;

                byte[] csUID = new byte[64];

                status = getUID(iID_RFID, csUID);
                switch (status)
                {
                    case 650:
                        cardID = String.Empty;
                        message = "Vui lòng đặt thẻ lên đầu đọc.";
                        return false;                        
                    case 651:
                    case 652:
                    case 708:
                    case 709:
                    case 711:
                    case 712:
                        cardID = TrimStringByte(Encoding.UTF8.GetString(csUID, 0, 64).Trim()); 
                        message = String.Empty;
                        return true;                            
                    default:
                        cardID = String.Empty;
                        message = "Lỗi kết nối thiết bị.";
                        return false;                       
                }                          
            }
            catch
            {
                cardID = String.Empty;
                message = "Vui lòng kiểm tra thiết bị đọc thẻ.";
                return false;                 
            }
        }

        public static bool WritePlate(int iID_RFID, string plateNumber,ref string message)
        {
            try
            {
                int status;
                byte[] csPlate_Number = StringToBytes(plateNumber.Trim());
                DateTime date = SqlHelper.SystemDate;
                byte[] csTime_Info = new byte[64];
                csTime_Info[0] = (byte)date.Hour;
                csTime_Info[1] = (byte)date.Minute;
                csTime_Info[2] = (byte)date.Day;
                csTime_Info[3] = (byte)date.Month;
                csTime_Info[4] = (byte)(date.Year % 100);
                csTime_Info[5] = 10;
                csTime_Info[6] = 13;
                csTime_Info[7] = 10;
                byte[] csDriver = new byte[64];
                byte[] csVehicle = new byte[64];
                status = write_plate(iID_RFID, csPlate_Number, csTime_Info, csDriver, csVehicle);
                message = String.Empty;
                switch (status)
                {
                    case 653:
                        return true;                              
                    case 701:                    
                    case 702:
                    case 703:
                    case 708:
                    case 709:
                    case 710:
                        message = "Lỗi kết nối thiết bị";
                        return false;
                    default:                        
                        message = "Lỗi ghi thẻ.\nVui lòng đổi thẻ khác";
                        return false;
                }
            }
            catch (Exception)
            {
                message = "Lỗi ghi thẻ.\n(Lỗi hệ thống)";
                return false;
            }
        }
    }
}
